package avriServer.command;

import avriServer.json.JSONArray;
import avriServer.json.JSONException;
import avriServer.json.JSONStringer;

/**
 * Buff commands, and result the list of command under the form of a JSON array.
 */
public class CommandBuffer {
	private StringBuffer commands = new StringBuffer();
	private int numberOfCommand = 0 ;
	/**
	 * Return a String of command using _functionName and _functionArguments.
	 * e.g: 
	 * <code>
	 * CommandBuffer.getCommand("say", "hello") ; //return {functionName: "say", attributes: ["hello"]}
	 * </code>
	 * @param _functionName
	 * @param _functionArguments
	 * @return
	 */
	public static String getCommand (String _functionName, Object... _functionArguments) {
		try {
			return new JSONStringer()
			.object()
				.key("functionName")
				.value(_functionName)
				.key("attributes")
				.value(new JSONArray(_functionArguments))
			.endObject()
			.toString();
		} catch (JSONException e) {
			return "";
		}
	}
	/**
	 * Return a String of command using _functionName
	 * e.g: 
	 * <code>
	 * CommandBuffer.getCommand("say") ; //return {functionName: "say", attributes: []}
	 * </code>
	 * @param _functionName
	 * @param _functionArguments
	 * @return
	 */
	public static String getCommand (String _functionName) {
		try {
			return new JSONStringer()
			.object()
				.key("functionName")
				.value(_functionName)
				.key("attributes")
				.value(new JSONArray())
			.endObject()
			.toString();
		} catch (JSONException e) {
			return "";
		}
	}
	
	public CommandBuffer () {		
	}

	public CommandBuffer (String _functionName, Object... _functionArguments) {
		this.append(_functionName, _functionArguments);
	}

	/**
	 * Append a command to the command list
	 * @param _functionName
	 * @param _functionArguments
	 */
	public void append (String _functionName, Object... _functionArguments) {
		this.numberOfCommand++;
		this.commands.append(CommandBuffer.getCommand(_functionName, _functionArguments)+",");
	}
		
	/**
	 * @return the commands as a JSONarray object
	 * e.g:
	 * <code>
	 * [{"functionName":"join","attributes":["general"]}, {"functionName":"join","attributes":["channel1"]}]
	 * </code>
	 */
	public String getCommands () {
		if (this.commands.length() < 1)
			return "";
		return "["+this.commands.substring(0, this.commands.length()-1)+"]";
	}
	
	/**
	 * @return the commands as a part of a JSONarray object
	 * It does exactly the same than getCommands but without the brackets
	 * e.g:
	 * <code>
	 * {"functionName":"join", "attributes":["general"]}, {"functionName":"join","attributes":["channel1"]}
	 * </code>
	 */
	public String getCommandsAsPartOfArray () {
		if (this.commands.length() < 1)
			return "";
		return this.commands.substring(0, this.commands.length()-1) ;
	}

	public void append(CommandBuffer _commandBuffer) {
		this.numberOfCommand++;
		this.commands.append(_commandBuffer.getCommandsAsPartOfArray()+",");
	}
	
	public int getNumberOfCommand () {
		return this.numberOfCommand  ;
	}
}